﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TZsTank : ControllerBase
{
    public override PlayerData GetPlayerData()
    {
        return new PlayerData()
        {
            Name = "团长的坦克",
            Author = "Jojo",
            BaseImage = "Sprite/Base/Base01",
            GunImage = "Sprite/Gun/Gun01"
        };
    }

    public override void Start()
    {
        base.Start();
        TurnTo(100, new Vector3(0, 0, 0));
        Looping = true;
        CoroutineMono.StartCoroutine(LoopAction());
    }

    private bool Looping;
    private float AngleTemp;

    public override IEnumerator LoopAction()
    {
        yield return new WaitForSeconds(2);

        while (Looping)
        {
            AngleTemp += 60;
            Move(1, Random.Range(0, 360), Random.Range(0.5f, 1f));
            Turn(AngleTemp, -100);
            yield return new WaitForSeconds(Random.Range(0.5f, 1f));
        }
        yield return 0;
    }

    public override void OnCrashWall()
    {
        CoroutineMono.StartCoroutine(CrashWall());
    }

    private IEnumerator CrashWall()
    {
        Looping = false;
        Stop();
        MoveTo(1, Vector2.zero);
        yield return new WaitForSeconds(2);
        Looping = true;
        CoroutineMono.StartCoroutine(LoopAction());
    }

    public override void OnCrashOthers(Vector3 othersPosition, float othersSpeed)
    {
    }

    Coroutine Ohit;
    public override void OnHitted(float bulletAngle)
    {
        if ((Mathf.Abs(bulletAngle - Data.currentAngle) > 90) || ScanedEnemy)
            return;
        StopTurn();
        Stop();
        if (Ohit != null)
            CoroutineMono.StopCoroutine(Ohit);
        Ohit = CoroutineMono.StartCoroutine(Onhit(bulletAngle));
    }
    private IEnumerator Onhit(float bulletAngle)
    {
        Looping = false;
        Move(1, bulletAngle, 2f);
        Turn(bulletAngle+180, 100);
        yield return new WaitForSeconds(2);
        Looping = true;
    }

    private bool ScanedEnemy;
    private Coroutine ScanC;
    public override void OnScanOthers(ScanOthersData scanOthersData)
    {
        if (scanOthersData.IsEnemy)
        {
            if (ScanC != null)
                CoroutineMono.StopCoroutine(ScanC);
            ScanC = CoroutineMono.StartCoroutine(ScanOthers(scanOthersData));
        }
    }

    private IEnumerator ScanOthers(ScanOthersData scanOthersData)
    {
        ScanedEnemy = true;
        Looping = false;
        Stop();
        StopTurn();
        Move(scanOthersData.othersSpeed.magnitude,scanOthersData.othersSpeed.ToAngle(), 1);
        if (scanOthersData.othersSpeed.magnitude < 0.2f)
        {
            Stop();
            Fire(2, 5);
        }
        else
        {
            if (Mathf.Abs(scanOthersData.othersAngle - Data.currentAngle) < 90)
            {
                Fire(3, 3);
            }
            else
            {
                Fire(10, 1);
            }
        }

        yield return new WaitForSeconds(1);
        Turn(Data.currentAngle - 30, -100);
        yield return new WaitForSeconds(1);
        Turn(Data.currentAngle + 60, 100);
        yield return new WaitForSeconds(1);
        StopTurn();
        Looping = true;
        ScanedEnemy = false;
        CoroutineMono.StartCoroutine(LoopAction());
    }

    public override void OnScaned(float angle)
    {
        Stop();
        Move(1, angle + 90, 3f);
    }

    public override void Update()
    {
    }
}